package com.github.easyms.server.config.shiro;

import com.github.easyms.common.constant.GlobalConstants;
import com.github.easyms.common.entity.auth.User;
import com.github.easyms.common.property.EasymsProperties;
import com.github.easyms.server.config.redis.RedisService;
import com.github.easyms.server.service.auth.UserService;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Leo.Liao
 * @Description:
 * @date 2020-04-09 22:42:18
 */
@Component
public class EasymsRealm extends AuthorizingRealm {

    @Resource
    private JwtService jwtService;
    @Resource
    private RedisService redisService;
    @Resource
    private EasymsProperties easymsProperties;
    @Resource
    private UserService userService;

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 认证
     *
     * @param auth
     * @return
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) {

        String token = (String) auth.getPrincipal();
        if(!jwtService.verify(token)){
            // Token 无效
            return null;
        }

        String claim = jwtService.getClaim(token, GlobalConstants.SecurityKey.USER);
        if(StringUtils.isEmpty(claim)){
            // Token 中无法获取用户信息
            return null;
        }

        String userId = (String) redisService.get(token);
        if(StringUtils.isEmpty(userId)){
            // 缓存中没有用户信息
            return null;
        }

        User user = userService.getActiveUserById(userId);
        if(null == user){
            // 数据库中没有用户信息（被禁用或者被删除）
            return null;
        }

        if(jwtService.isAboutToExpire(token,easymsProperties.getTokenExpireTime())){
            // Token即将过期，刷新过期时间
            redisService.set(token, claim, easymsProperties.getTokenExpireTime());
            redisService.set(claim, token, easymsProperties.getTokenExpireTime());
        }

        return new SimpleAuthenticationInfo(user,token, "easymsRealm");
    }


    /**
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//
//        User currentInfo = userService.getCurrentUser();
//        if(currentInfo == null){
//            return authorizationInfo;
//        }
//        //获取role
//        Role role = roleService.selectById(currentInfo.getRoleId());
//        //获取权限
//        authorizationInfo.addRole(role.getEnName());
//        List<Menu> authorityList = menuMapper.queryByRole(renaProperties.getDefaultLang(),role.getRoleId());
//        for (Menu menu : authorityList) {
//            authorizationInfo.addStringPermission(menu.getTextCode());
//        }
        return null;
    }

}
